package com.example.competionmanagebackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.competionmanagebackend.entity.Competitions;
import com.example.competionmanagebackend.entity.Registrations;
import com.example.competionmanagebackend.service.CompetitionsService;
import com.example.competionmanagebackend.service.RegistrationsService;
import com.example.competionmanagebackend.utils.PageRequest;
import com.example.competionmanagebackend.utils.PageResult;
import com.example.competionmanagebackend.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/registrations")
public class RegistrationsController {

    @Autowired
    private RegistrationsService registrationsService;

    @Autowired
    private CompetitionsService competitionsService;

    /**
     * 分页查询报名信息，同时为每条报名记录设置对应竞赛的名称。
     */
    @PostMapping("/getRegistrationsPage")
    public ResponseEntity<Result<PageResult<Registrations>>> getRegistrationsPage(@RequestBody PageRequest pageRequest) {
        Page<Registrations> page = registrationsService.getRegistrationsPage(
                pageRequest.getCurrentPage(),
                pageRequest.getPageSize()
        );
        // 查询所有竞赛，方便为报名记录关联竞赛名称
        List<Competitions> allCompetitions = competitionsService.list();
        page.getRecords().forEach(reg -> {
            Competitions comp = allCompetitions.stream()
                    .filter(c -> c.getId().equals(reg.getCompetitionId()))
                    .findFirst()
                    .orElse(null);
            if (comp != null) {
                reg.setCompetitionName(comp.getCompetitionName());
            }
        });
        PageResult<Registrations> result = new PageResult<>(page);
        return Result.success(result);
    }

    /**
     * 根据报名ID获取报名详情，同时补充竞赛名称。
     */
    @GetMapping("/detail")
    public ResponseEntity<Result<Registrations>> getRegistrationDetail(@RequestParam("id") Integer id) {
        Registrations registration = registrationsService.getRegistrationById(id);
        if (registration == null) {
            return Result.fail(400, "报名信息不存在");
        }
        Competitions comp = competitionsService.getById(registration.getCompetitionId());
        if (comp != null) {
            registration.setCompetitionName(comp.getCompetitionName());
        }
        return Result.success(registration);
    }

    /**
     * 新增或修改报名信息。
     */
    @PostMapping("/edit")
    public ResponseEntity<Result<Registrations>> editRegistration(@RequestBody Registrations registration) {
        if (registration.getRegistrationId() == null) {
            registrationsService.insertRegistration(registration);
        } else {
            registrationsService.updateRegistration(registration);
        }
        return Result.success(registration);
    }

    /**
     * 删除报名信息。
     */
    @GetMapping("/delete")
    public ResponseEntity<Result<String>> deleteRegistration(@RequestParam("id") Integer id) {
        registrationsService.deleteRegistration(id);
        return Result.success("删除成功");
    }
}